﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using ElingOA6_Admin_DSJCJ.Models;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 用品类别
    /// </summary>
    /// 
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class OfficeItemClsController : AdminBaseController
    {
        //
        // GET: /Admin/OfficeItemCls/
        /// <summary>
        /// 用品类别主页
        /// </summary>
        /// <returns>视图</returns>
        [AuthorityActionFilter("m_OfficeItemCls", Order = 1)]
        [LogPageActionFilter("用品类别", Order = 2)]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 类别树
        /// </summary>
        /// <returns>视图</returns>
        public ActionResult OfficeItemClsTree()
        {
            TreeModelList treeModelList = new TreeModelList();
            BLL.ItemCls itemClsBll = new BLL.ItemCls();
            List<Model.ItemCls> itemClsAllList = itemClsBll.GetModelList("");
            TreeItemModel treeParentItemModel = new TreeItemModel(null)
            {
                Id = "0",
                Name = "用品类别列表",
                ImageUrl = Url.Content("~/Content/ControlImages/scripts_code.png")

            };
            List<Model.ItemCls> itemClsList = itemClsAllList.ToList();

            if (itemClsList.Count > 0)
            {
                foreach (Model.ItemCls itemCls in itemClsList)
                {
                    TreeItemModel treeSonItemModel = new TreeItemModel(treeParentItemModel)
                    {
                        Id = itemCls.FItemClsNo,
                        Name = itemCls.FItemClsName,
                        ImageUrl = Url.Content("~/Content/ControlImages/scripts_code.png")

                    };

                    treeParentItemModel.Id = "%!^" + treeParentItemModel.Id;

                }
            }
            treeModelList.Add(treeParentItemModel);

            return View(treeModelList);
        }


        /// <summary>
        /// 右边的界面
        /// </summary>
        /// <param name="itemClsNo">用品编号</param>
        /// <returns>视图</returns>
        public ActionResult OfficeItemClsEdit(string itemClsNo)
        {
            Model.ItemCls itemClsModel = new Model.ItemCls();
            if (itemClsNo == "" || itemClsNo == null)
            {
                ViewData["type"] = "ADD";
                itemClsModel.FItemClsNo = "";
                itemClsModel.FItemClsName = "";
                ViewData["Cls"] = true;
            }
            else
            {
                ViewData["type"] = "Edit";
                ViewData["Cls"] = false;
                itemClsNo = Fetch.estimateString(itemClsNo, CheckGetEnum.Safety);
                BLL.ItemCls itemClsBll = new BLL.ItemCls();
                string entityNo = GlobalConstant.ENTITYNO;
                itemClsModel = itemClsBll.GetModel(itemClsNo, entityNo);
                if (itemClsModel == null) 
                {
                    itemClsModel = new Model.ItemCls();
                }
                
            }
            return View(itemClsModel);
        }


        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="itemClsModel">itemClsModel对象</param>
        /// <returns>视图</returns>
        public ActionResult Save(Model.ItemCls itemClsModel)
        {           
            itemClsModel.FItemClsNo = Fetch.Post("FItemClsNo").Trim();
            itemClsModel.FItemClsName = Fetch.Post("FItemClsName").Trim();
            itemClsModel.FEntityNo = GlobalConstant.ENTITYNO;
            
            BLL.ItemCls itemClsBll = new BLL.ItemCls();
            string entityNo = GlobalConstant.ENTITYNO;
            string itemClsNo = Fetch.Post("FItemClsNo").Trim();
            string type = Fetch.Post("hdtype");
            if (!"".Equals(itemClsModel.FItemClsNo) && !"".Equals(itemClsModel.FItemClsName))
            {
                if (type == "ADD")
                {
                    if (itemClsBll.Exists(itemClsNo, entityNo))
                    {
                        ViewData["jsAlert"] = "对不起，该用品类别编号已存在！";
                    }
                    else
                    {
                        itemClsBll.Add(itemClsModel);

                        Response.Write("<script>alert('操作成功');parent.window.location.href = '" + Url.Action("Index", "OfficeItemCls") + "'</script>");
                        Response.Flush();
                        Response.End();
                    }

                }
                else if (type == "Edit")
                {
                    itemClsBll.Update(itemClsModel);

                    Response.Write("<script>alert('操作成功');parent.window.location.href = '" + Url.Action("Index", "OfficeItemCls") + "'</script>");
                    Response.Flush();
                    Response.End();
                }
            }
            else {
                ViewData["jsAlert"] = "对不起，输入项不能为空！";
                
            }
            return View("OfficeItemClsEdit", itemClsModel);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="itemClsNo">用品编号</param>
        /// <returns>返回字符判断是否删除</returns>
        public string Delete(string itemClsNo)
        {
            BLL.ItemCls itemClsBll = new BLL.ItemCls();
            BLL.Item itemBll = new BLL.Item();
            string entityNo = GlobalConstant.ENTITYNO;
            string str = "";
            try {
                if (itemBll.ExistsCls(itemClsNo))
                {
                    str = "exists";
                }
                else {
                    itemClsBll.Delete(itemClsNo, entityNo);
                    str = "ok";
                }
            }
            catch(Exception) {
                str = "error";
            }

            return str;
        }

    }
}
